package com.bush.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.product.mapper.ProductAttrValueMapper;
import com.bush.mall.product.entity.ProductAttrValueEntity;
import com.bush.mall.product.service.ProductAttrValueService;

/**
 * spu属性值
 * @author RUN
 * @date 2022-11-28
 */
@Service(ProductAttrValueService.BEAN_ID)
@Slf4j
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueMapper, ProductAttrValueEntity> implements ProductAttrValueService {

    /**
     * 查询spu属性值列表
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<ProductAttrValueEntity> page = new Page<>(current,size);
            QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("ProductAttrValueService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建spu属性值表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result saveProductAttrValue(ProductAttrValueEntity productAttrValue) {
        try{
            Assert.notNull(productAttrValue,"参数异常");
            boolean flag = this.save(productAttrValue);
            if (flag) return Result.ok("spu属性值添加成功");
            throw new SaaSException("spu属性值添加失败");
        }catch(Exception e){
            log.error("ProductAttrValueService saveProductAttrValue ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改spu属性值表单对象
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result updateProductAttrValue(ProductAttrValueEntity productAttrValue) {
        try{
            Assert.notNull(productAttrValue,"参数异常");
            boolean flag = this.updateById(productAttrValue);
            if (flag) return Result.ok("spu属性值修改成功");
            throw new SaaSException("spu属性值修改失败");
        }catch(Exception e){
            log.error("ProductAttrValueService updateProductAttrValue ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除spu属性值表单
     * @author RUN
     * @date 2022-11-28
     */
    @Override
    public Result deleteProductAttrValue(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("spu属性值删除成功");
            throw new SaaSException("spu属性值删除失败");
        }catch(Exception e){
            log.error("ProductAttrValueService deleteProductAttrValue ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}